#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "module/module_window.h"
#include "module/property/module_port_property.h"

#include "head_button/dialog_new_module.h"

#include <QDebug>

MainWindow* MainWindow::mywindow = nullptr;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    mywindow = this;

}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_new_module_clicked()
{
    new_module();
}

/*
 * Name: newModule
 *
 *
 *
 */

void MainWindow::new_module()
{
    dialog_new_module new_module_dialog;
    new_module_dialog.show();
    if (new_module_dialog.exec() == QDialog::Accepted)
    {
        QString name = new_module_dialog.get_name();

        ModulePorts mouduleports;
        mouduleports.Input_Ports = new_module_dialog.get_inout(dialog_new_module::input);
        mouduleports.Output_Ports = new_module_dialog.get_inout(dialog_new_module::output);
        mouduleports.InOut_Ports = new_module_dialog.get_inout(dialog_new_module::inout);

        Margin margin;
        margin.leftMargin = 100;
        margin.topMargin = 50;
        margin.rightMargin = 100;
        margin.bottonMargin = 50;

        module_window *new_window = new module_window(name, mouduleports, margin, this);

        ui->tabWidget->addTab(new_window, name);

        ui->tabWidget->setCurrentIndex(ui->tabWidget->count() - 1); // 将tab移动至最后新增的tab上去

        new_window->show();

        new_window->init();
    }
}

void MainWindow::insert_text2textEdit(QString text)
{
    ui->textEdit->setText(text);
}

